#!/bin/bash
# Hive Hook部署脚本

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

echo_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

echo_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查HIVE_HOME
if [ -z "$HIVE_HOME" ]; then
    echo_error "HIVE_HOME 环境变量未设置"
    exit 1
fi

echo_info "HIVE_HOME: $HIVE_HOME"

# 进入项目目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$SCRIPT_DIR/long_term"

# 1. 编译项目
echo_info "编译Hive Hook项目..."
if ! command -v mvn &> /dev/null; then
    echo_error "未找到Maven，请先安装Maven"
    exit 1
fi

mvn clean package -DskipTests

if [ $? -ne 0 ]; then
    echo_error "编译失败"
    exit 1
fi

JAR_FILE="target/hive-metadata-monitor-hook-1.0.0.jar"
if [ ! -f "$JAR_FILE" ]; then
    echo_error "未找到JAR文件: $JAR_FILE"
    exit 1
fi

echo_info "编译成功: $JAR_FILE"

# 2. 部署JAR到Hive
echo_info "部署JAR到Hive..."
cp "$JAR_FILE" "$HIVE_HOME/lib/"

if [ $? -ne 0 ]; then
    echo_error "部署JAR失败"
    exit 1
fi

echo_info "JAR部署成功"

# 3. 配置hive-site.xml
echo_info "配置hive-site.xml..."

HIVE_SITE="$HIVE_HOME/conf/hive-site.xml"

if [ ! -f "$HIVE_SITE" ]; then
    echo_error "未找到hive-site.xml: $HIVE_SITE"
    exit 1
fi

# 备份原配置
cp "$HIVE_SITE" "$HIVE_SITE.backup.$(date +%Y%m%d%H%M%S)"
echo_info "已备份原配置文件"

# 检查是否已配置Hook
if grep -q "com.company.hive.monitor.MetadataMonitorHook" "$HIVE_SITE"; then
    echo_warn "Hook已配置，跳过配置步骤"
else
    echo_info "添加Hook配置到hive-site.xml..."
    echo ""
    echo_warn "请手动添加以下配置到 $HIVE_SITE："
    echo ""
    cat <<'EOF'
<!-- Hive元数据监控Hook配置 -->
<property>
    <name>hive.metastore.event.listeners</name>
    <value>com.company.hive.monitor.MetadataMonitorHook</value>
    <description>Hive元数据监控Hook</description>
</property>

<property>
    <name>hive.metastore.monitor.alert.enabled</name>
    <value>true</value>
    <description>是否启用告警</description>
</property>

<property>
    <name>hive.metastore.monitor.alert.async</name>
    <value>true</value>
    <description>是否异步发送告警</description>
</property>

<property>
    <name>hive.metastore.monitor.feishu.webhook</name>
    <value>https://open.feishu.cn/open-apis/bot/v2/hook/YOUR_WEBHOOK_URL</value>
    <description>飞书Webhook URL</description>
</property>

<property>
    <name>hive.metastore.monitor.drop.table.whitelist</name>
    <value>tmp_*,test_*</value>
    <description>删表白名单</description>
</property>

<property>
    <name>hive.metastore.monitor.drop.table.blacklist</name>
    <value>production_db.*,*.fact_*</value>
    <description>删表黑名单</description>
</property>

<property>
    <name>hive.metastore.monitor.drop.database.whitelist</name>
    <value>test_*,tmp_*</value>
    <description>删库白名单</description>
</property>

<property>
    <name>hive.metastore.monitor.overwrite.time.window</name>
    <value>300000</value>
    <description>表覆盖检测时间窗口（毫秒）</description>
</property>
EOF
    echo ""
fi

# 4. 重启MetaStore提示
echo ""
echo "============================================"
echo_info "部署完成！"
echo ""
echo "下一步操作："
echo "1. 检查配置文件："
echo "   vi $HIVE_SITE"
echo "   确认已添加Hook配置，并修改飞书Webhook URL"
echo ""
echo "2. 重启Hive MetaStore服务："
echo "   $HIVE_HOME/bin/hive --service metastore stop"
echo "   $HIVE_HOME/bin/hive --service metastore start"
echo ""
echo "3. 验证Hook是否加载："
echo "   grep 'MetadataMonitorHook' $HIVE_HOME/logs/hive-metastore.log"
echo ""
echo "4. 测试告警（在测试环境）："
echo "   hive -e \"DROP TABLE IF EXISTS test_db.test_table\""
echo "   检查是否收到飞书告警"
echo "============================================"

